package com.chezai.client.activitys;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chezai.client.R;
import com.chezai.client.adapter.SpeechTestAdapter;
import com.chezai.client.bean.Speech;
import com.chezai.client.bean.SpeechInitEvent;
import com.chezai.client.utils.SpeechUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

/**
 * 语音测试
 * Created by likunyang on 2018/2/1.
 */

public class SpeechTestActivity extends BaseActivity {
    @BindView(R.id.rv)
    RecyclerView rv;// 缓冲进度
    @BindView(R.id.tv_error)
    TextView tvError;
    private SpeechTestAdapter mAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_speech_test;
    }

    @Override
    protected void onResume() {
        super.onResume();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        List<Speech> list = Arrays.asList(Speech.values());
        mAdapter = new SpeechTestAdapter(R.layout.item_speech_test, list);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(mAdapter);

        if (SpeechUtils.getInstance().isInitSucess()) {
            tvError.setVisibility(View.GONE);
        } else {
            int errorCode = SpeechUtils.getInstance().getInitErrorCode();
            tvError.setText("语音服务初始化失败，可能会影响语音功能正常使用，错误码：" + errorCode + ",点击重试");
            tvError.setVisibility(View.VISIBLE);
            tvError.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SpeechUtils.getInstance().reset();
                }
            });
        }
    }

    @Override
    public void initData() {

    }

    @Subscribe
    public void onEventSpeechInit(SpeechInitEvent speech) {
        if (speech.sucess) {
            showToast("语音初始化成功");
            tvError.setVisibility(View.GONE);
            List<Speech> list = Arrays.asList(Speech.values());
            mAdapter = new SpeechTestAdapter(R.layout.item_speech_test, list);
            rv.setAdapter(mAdapter);
        } else {
            tvError.setText("语音服务初始化失败，可能会影响语音功能正常使用，错误码：" + speech.code + ",点击重试");
        }

    }
}
